//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package roadmap_state_api_serv

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao"
	"atomgit.com/openlinksaas-org/api-server/dao/roadmap_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/user_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/roadmap_state_api"
)

func (apiImpl *RoadmapStateApiImpl) ListMyLog(ctx context.Context, req *roadmap_state_api.ListMyLogRequest) (*roadmap_state_api.ListMyLogResponse, error) {
	emptyList := []*roadmap_state_api.StateOpItem{}

	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_state_api.ListMyLogResponse{
			Code:   roadmap_state_api.ListMyLogResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
			OpList: emptyList,
		}, nil
	}
	count, err := roadmap_dao.UserOpIndexDao.Count(ctx, sessionItem.UserId)
	if err != nil {
		return nil, err
	}
	opIdList, err := roadmap_dao.UserOpIndexDao.List(ctx, sessionItem.UserId, int64(req.Offset), int64(req.Limit))
	if err != nil {
		return nil, err
	}
	opItemList, err := apiImpl.listOpItem(ctx, sessionItem.UserId, opIdList)
	if err != nil {
		return nil, err
	}
	return &roadmap_state_api.ListMyLogResponse{
		Code:       roadmap_state_api.ListMyLogResponse_CODE_OK,
		TotalCount: count,
		OpList:     opItemList,
	}, nil
}

func (apiImpl *RoadmapStateApiImpl) listOpItem(ctx context.Context, ownerUserId string, opIdList []string) ([]*roadmap_state_api.StateOpItem, error) {
	opItemList, err := roadmap_dao.StateOpDao.ListById(ctx, opIdList)
	if err != nil {
		return nil, err
	}
	userIdList := []string{}
	roadmapIdList := []string{}
	for _, opItem := range opItemList {
		userIdList = append(userIdList, opItem.UserId)
		roadmapIdList = append(roadmapIdList, opItem.RoadmapId)
	}
	//获取用户信息
	userItemList, err := user_dao.UserInfoDao.ListById(ctx, userIdList)
	if err != nil {
		return nil, err
	}
	userItemMap := map[string]*user_dao.UserInfoDbItem{}
	for _, userItem := range userItemList {
		userItemMap[userItem.UserId] = userItem
	}
	//检查路线图是否存在
	roadmapItemList, err := roadmap_dao.RoadmapInfoDao.ListById(ctx, roadmapIdList)
	if err != nil {
		return nil, err
	}
	roadmapItemMap := map[string]*roadmap_dao.RoadmapInfoDbItem{}
	for _, roadmapItem := range roadmapItemList {
		roadmapItemMap[roadmapItem.RoadmapId] = roadmapItem
	}

	//生成结果
	retItemList := []*roadmap_state_api.StateOpItem{}
	for _, opItem := range opItemList {
		retItem := opItem.ToStateOpItem()
		userItem, ok := userItemMap[opItem.UserId]
		if ok {
			retItem.UserDisplayName = userItem.BasicInfo.DisplayName
			retItem.UserLogoUri = userItem.BasicInfo.LogoUri
		}
		roadmapItem, ok := roadmapItemMap[opItem.RoadmapId]
		if ok {
			retItem.RoadmapExist = roadmapItem.PubState || roadmapItem.OwnerUserId == ownerUserId
			retItem.CanUpdateRoadmap = (!roadmapItem.PubState && roadmapItem.OwnerUserId == ownerUserId)
		}
		retItemList = append(retItemList, retItem)
	}

	return retItemList, nil
}
